package com.situ.day7;

import org.junit.Test;

public class HomeWork {
    @Test
    public void test1() {
        //java.lang.StringIndexOutOfBoundsException: String index out of range: 6
        //String str = "---Java Android---";
        //String str = "------";
        //String str = "";
        //String str = null;
        //String str = "Java Android---";
        //String str = "---Java Android";
        //String str = "Java Android";
        String str = "";
        String newStr = trim(str);
        System.out.println(newStr);
    }

    private String trim(String str) {
        if (str == null) {
            return "";
        }
        int startIndex = 0;
        int endIndex = str.length() - 1;
        while ((startIndex <= endIndex) && (str.charAt(startIndex) == '-')) {
            startIndex++;
        }
        while ((startIndex <= endIndex) && (str.charAt(endIndex) == '-')) {
            endIndex--;
        }

        return str.substring(startIndex, endIndex + 1);
    }


    @Test
    public void test3() {
        String str = "Hello Java";
        System.out.println(str.toLowerCase());
        System.out.println(str.toUpperCase());
        String newStr = toLowerCase(str);
        System.out.println(newStr);
    }

    private String toLowerCase(String str) {
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] >= 'A' && chars[i] <= 'Z') {
                chars[i] = (char)(chars[i] + 32);
            }
        }
        return new String(chars);
    }
}
